package org.example.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

public class Demo02JDBC {
    public static void main(String[] args) throws Exception {
        //1.加载数据库驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.创建数据库的连接对象  jdbc:mysql://localhost:3306/tedu?
        Connection connection= DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        //3.创建sql语句的执行对象
        Statement statement=connection.createStatement();
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入需要修改密码的用户名");
        String username=sc.nextLine();
        System.out.print("请输入新密码：");
        String new_password=sc.nextLine();
        //4.执行sql语句  执行写操作（增删改）executeUpdate()    执行查操作executeQuery()
        //把外部变量值嵌入sql语句中   "+变量名+"
        int i = statement.executeUpdate("UPDATE user SET password='"+new_password+"'WHERE username='"+username+"'");
        if(i>0){
        System.out.println("密码修改成功");
        }else{
            System.out.println("无此用户");
        }
        //断开数据库连接
        connection.close();

    }
}
